package com.xuhuan.axios.controller;

import com.xuhuan.axios.entity.ArticleDTO;
import com.xuhuan.axios.response.Result;
import io.micrometer.common.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: huan.xu
 * @create: 2024/6/28 12:08
 * @description:
 **/
@RequestMapping("/article")
@RestController
@CrossOrigin
public class ArticleController {

  private List<ArticleDTO> articleList = new ArrayList<>();

  @GetMapping("/getALL")
  public Result getALL() {
    handArticleListData();
    return Result.success(articleList);
  }

  @GetMapping("/search")
  public Result search(String category, String state) {
    handArticleListData();
    List<ArticleDTO> articleDTOList = articleList.stream()
        .filter(articleDTO -> {
          if (StringUtils.isNotBlank(category)) {
            if (!articleDTO.getCategory().equals(category)) {
              return false;
            }
          }
          return true;
        })
        .filter(articleDTO -> {
          if (StringUtils.isNotBlank(state)) {
            if (!articleDTO.getState().equals(state)) {
              return false;
            }
          }
          return true;
        })
        .collect(Collectors.toList());
    return Result.success(articleDTOList);
  }

  private void handArticleListData() {
    articleList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      articleList.add(new ArticleDTO(i, "标题" + i, "分类" + i, "2024-6-2" + i, "状态" + i));
    }
  }

}
